package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.bo.WxAuthRegisterBo;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fyy
 * @since 2023/01/02 12:02
 */

@Service
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    AliyunComponent aliyunComponent;

    @Autowired
    MarketUserMapper userMapper;

    /**
     * 发送验证码
     * @param mobile
     * @param code
     */
    @Override
    public void regCaptcha(String mobile, String code) {
        aliyunComponent.sendMsg(mobile,code);
    }

    /**
     * 注册
     * @param registerBo
     */
    @Override
    public void register(WxAuthRegisterBo registerBo) {
        userMapper.insertSelective(registerBo);
    }

    /**
     * 修改密码
     * @param marketUser
     */
    @Override
    public void update(MarketUser marketUser) {
        userMapper.updateByPrimaryKeySelective(marketUser);
    }

    /**
     * 查询用户数量
     * @param registerBo
     * @return
     */
    @Override
    public int select(WxAuthRegisterBo registerBo) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        String mobile = registerBo.getMobile();
        //判断传入手机号是否为空
        if (!StringUtil.isEmpty(mobile)){
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        return marketUsers.size();
    }

    /**
     * 通过手机号修改密码
     * @param registerBo
     */
    @Override
    public void updateByMobile(WxAuthRegisterBo registerBo) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        String mobile = registerBo.getMobile();
        //判断手机号是否为空
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        userMapper.updateByExampleSelective(registerBo,example);
    }
}
